// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Piston Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 68 : TYPE_PISTON

// Function declarations
reserve function Piston_DebugDraw
reserve function Piston_DebugSpawn

// Static Values
public value Piston_value39 = 0;

// Tables
public table Piston_table45
	0x380000, 0x400000, 0x600000
end table


function Piston_DebugDraw
	temp0 = object[0].value17
	temp0 -= Piston_value39
	DrawSprite(0)
end function


function Piston_DebugSpawn
	temp0 = object[0].value17
	temp0 -= Piston_value39
	CreateTempObject(TypeName[Piston], temp0, object.xpos, object.ypos)
	GetTableValue(object[tempObjectPos].value0, object[tempObjectPos].propertyValue, Piston_table45)
end function


event ObjectMain
	if object.value2 != 0
		object.value2--
		object.yvel = 0
	else
		if object.direction == FLIP_NONE
			object.yvel = 0x80000
			object.value1 += object.yvel
			if object.value1 >= object.value0
				object.direction ^= FLIP_X
				if object.propertyValue != 0
					object.value2 = 60
				end if
			end if
		else
			if object.propertyValue != 0
				object.yvel = -0x80000
			else
				object.yvel = -0x10000
			end if
			object.value1 += object.yvel
			if object.value1 <= 0
				object.direction ^= FLIP_X
				object.value2 = 60
			end if
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_BOX2, object.entityPos, -28, -32, 28, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		switch checkResult
		case 1
			object[currentPlayer].ypos += object.yvel
			break
		case 4
			if object[currentPlayer].gravity == GRAVITY_GROUND
				if object.yvel > 0
					CallFunction(PlayerObject_Kill)
				end if
			end if
			break
		end switch
	next
	object.ypos += object.yvel
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-28, -32, 56, 64, 132, 1)
	foreach (TypeName[Piston], arrayPos0, ALL_ENTITIES)
		GetTableValue(object[arrayPos0].value0, object[arrayPos0].propertyValue, Piston_table45)
	next
	temp0 = 0
	Piston_value39 = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Piston], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Piston_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Piston_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
